package com.ak.sb.admin.controller.user;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ak.sb.domain.model.User;
import com.ak.sb.service.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/user")
public class UserController {

	private Logger logger = LoggerFactory.getLogger(UserController.class);

	boolean isDebugger = logger.isDebugEnabled();

	@Autowired
	private IUserService userService;

	/**
	 * 用户详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}")
	public User getUser(@PathVariable Long id) {
		User userStore = userService.getById(id);
		if (isDebugger) {
			logger.debug("user.name is {}", userStore.getName());
		}
		return userStore;
	}

	/**
	 * 用户列表
	 * 
	 * @param user
	 * @return
	 */
	@GetMapping("/list")
	public List<User> getUserList(User user) {
		LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda();
		queryWrapper.select(User::getId, User::getName, User::getEmail);

		queryWrapper.like(StringUtils.hasText(user.getName()), User::getName, user.getName());
		return userService.list(queryWrapper);
	}

	/**
	 * 分页展示用户列表
	 * 
	 * @param current
	 * @return
	 */
	@GetMapping("/entry/{current}")
	public IPage<User> withPagination(@RequestParam(value = "current", defaultValue = "1") Long current, User user) {

		LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda();
		wrapper.eq(StringUtils.hasText(user.getName()), User::getName, user.getName());
		wrapper.setEntity(user);
		IPage<User> iPage = new Page<User>();
		iPage.setCurrent(current);
		iPage.setSize(2);
		return userService.page(iPage, wrapper);
	}

	/**
	 * 保存
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping(value = "/save")
	public boolean save(User user) {

		boolean status = userService.save(user);
		if (isDebugger) {
			logger.debug("user save status is {}", status);
		}
		return status;
	}

	/**
	 * 根据Id更新
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping("/updateById")
	public boolean updateById(User user) {
		boolean updateStatus = userService.updateById(user);
		if (isDebugger) {
			logger.debug("user update status is {}", updateStatus);
		}
		return updateStatus;
	}

	/**
	 * 根据传入的条件更新
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping("/update")
	public boolean update(User user) {

		LambdaUpdateWrapper<User> updateWrapper = new UpdateWrapper<User>().lambda();

		updateWrapper.eq(User::getId, user.getId());
		updateWrapper.set(User::getAge, user.getAge());

		boolean updateStatus = userService.update(updateWrapper);
		if (isDebugger) {
			logger.debug("user update status is {}", updateStatus);
		}
		return updateStatus;
	}

	/**
	 * 根据Id删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public boolean delete(Long id) {

		boolean delete = userService.removeById(id);
		if (isDebugger) {
			logger.debug("user delete status is {}", delete);
		}
		return delete;
	}

}
